package com.yssoft.controller.app.equipment;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.DecoderException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.yssoft.entity.core.TCode;
import com.yssoft.entity.equipment.TEquipmentRecordPVO;
import com.yssoft.entity.equipment.TEquipmentRecordTVO;
import com.yssoft.service.equipment.EquipmentRecordTService;
import com.yssoft.utils.AppUserHelper;
import com.yssoft.utils.AppUtil;


@CrossOrigin//解决跨域session问题
@RestController
@RequestMapping(value = "/app/equipmentRecordT")
public class EquipmentRecordTController {
	private static Logger logger=Logger.getLogger(EquipmentRecordTController.class);
	
	@Resource
	private EquipmentRecordTService equipmentRecordTService;
	
	
	@ResponseBody
    @RequestMapping(value = "queryEquipmentRecordTList", method = RequestMethod.POST)
	public Object queryEquipmentRecordTList(HttpServletRequest request){
		String msg = "列表查询";
    	try{
    		Map<String,Object> params = new HashMap<String,Object>();
    		params.put("tEquipmentTId", request.getParameter("tEquipmentTId"));
    		params.put("tCId", request.getParameter("tCId"));
    		params.put("id", request.getParameter("id"));
    		int page = Integer.valueOf(request.getParameter("page"));
    		int limit = Integer.valueOf(request.getParameter("limit"));
    		int start = ((page - 1) * limit);
    		int end = limit;
    		params.put("start", start);
    		params.put("end", end);
    		List<Map<String,Object>> pageList = equipmentRecordTService.pageList(params);
        	return AppUtil.returnListJson("1", msg+"成功!", pageList);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnListJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 生产设备维护记录保存
	 */
	@ResponseBody
    @RequestMapping(value = "saveEquipmentRecordT", method = RequestMethod.POST)
	public Object saveEquipmentRecordT(TEquipmentRecordTVO equipmentRecordT,HttpServletRequest request){
		String msg = "检验设备维护记录保存";
    	try{
    		equipmentRecordT.setCreatorId(AppUserHelper.getCurrentUserId(request));
    		equipmentRecordT.setCreatorName(AppUserHelper.getCurrentUserName(request));
    		equipmentRecordT.setDeptId(AppUserHelper.getCurrentDeptId(request));
    		equipmentRecordT.setDeptName(AppUserHelper.getCurrentDeptName(request));
    		equipmentRecordT.settEId(AppUserHelper.getCurrentCompanyId(request));
    		MultipartRequest mRequest = (MultipartRequest)request;
    		this.equipmentRecordTService.insertSelective(equipmentRecordT,mRequest);
        	return AppUtil.returnMapJson("1", msg+"成功!", null);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	/**
	 * 生产设备维护记录保存
	 */
	@ResponseBody
    @RequestMapping(value = "updateEquipmentRecordT", method = RequestMethod.POST)
	public Object updateEquipmentRecordT(TEquipmentRecordTVO equipmentRecordT,HttpServletRequest request){
		String msg = "检验设备维护记录修改";
    	try{
    		MultipartRequest mRequest = (MultipartRequest)request;
    		this.equipmentRecordTService.updateOne(equipmentRecordT,mRequest);
        	return AppUtil.returnMapJson("1", msg+"成功!", null);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
    @ResponseBody
    @RequestMapping(value = "deleteEquipmentRecordT",method = RequestMethod.POST)
    public Object deleteEquipmentRecordT(Integer id) {
    	String msg = "检验设备维护记录删除";
         try {
        	 this.equipmentRecordTService.deleteOne(id);
        	 return AppUtil.returnMapJson("1", msg+"成功!", null);
         } catch (NumberFormatException e) {
        	 logger.info(msg+"失败："+e.getMessage());
     		return AppUtil.returnMapJson("0", msg+"失败!", null);
         }
    }

	@RequestMapping(value = "saveEquipmentRecordT1", method = RequestMethod.POST)
    public Object saveEquipmentRecordT1(TEquipmentRecordTVO equipmentRecordT, HttpServletRequest request, @RequestParam("files") MultipartFile[] files) throws IOException, DecoderException {
		String msg = "检验设备维护记录保存";
    	try{
    		equipmentRecordT.setCreatorId(AppUserHelper.getCurrentUserId(request));
    		equipmentRecordT.setCreatorName(AppUserHelper.getCurrentUserName(request));
    		equipmentRecordT.setDeptId(AppUserHelper.getCurrentDeptId(request));
    		equipmentRecordT.setDeptName(AppUserHelper.getCurrentDeptName(request));
    		equipmentRecordT.settEId(AppUserHelper.getCurrentCompanyId(request));
    		MultipartRequest mRequest = (MultipartRequest)request;
    		this.equipmentRecordTService.insertSelective1(equipmentRecordT,mRequest,files);
        	return AppUtil.returnMapJson("1", msg+"成功!", null);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	} 
	
	@RequestMapping(value = "updateEquipmentRecordT1", method = RequestMethod.POST)
    public Object updateEquipmentRecordT1(TEquipmentRecordTVO equipmentRecordT, HttpServletRequest request, @RequestParam("files") MultipartFile[] files) throws IOException, DecoderException {
		String msg = "检验设备维护记录修改";
    	try{
    		MultipartRequest mRequest = (MultipartRequest)request;
    		this.equipmentRecordTService.updateOne1(equipmentRecordT,mRequest,files);
        	return AppUtil.returnMapJson("1", msg+"成功!", null);
    	} catch (Exception e) {
    		logger.info(msg+"失败："+e.getMessage());
    		return AppUtil.returnMapJson("0", msg+"失败!", null);
		}
	}
	
	@RequestMapping(value = "getEquipmentMaintType",method = RequestMethod.POST)
    @ResponseBody
    public Object getEquipmentMaintType() {
         try {
             List<TCode> codeList = equipmentRecordTService.findEquipmentMaintType();
			 List<Map<String, Object>> equipmentMaintList = new ArrayList<Map<String,Object>>();
			 for (TCode map : codeList) {
				Map<String, Object> code = new HashMap<String, Object>();
				code.put("id", map.getId());
				code.put("name", map.getDescribed());
				code.put("classify", map.getClassify());
				equipmentMaintList.add(code);
			}
			return AppUtil.returnListJson("1", "查询成功!", equipmentMaintList);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
         }
    }
	
    @RequestMapping(value = "getOne",method = RequestMethod.POST)
    @ResponseBody
    public Object getOne(Integer id) {
         try {
             return AppUtil.returnMapJson("1", "查询成功!", equipmentRecordTService.getOne(id));
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
         }
    }	
    
}
